local huoshi = fk.CreateSkill{
  name = "rainveil__huoshi",
}

Fk:loadTranslationTable{
  ["rainveil__huoshi"] = "惑世",
  [":rainveil__huoshi"] = "每轮每种花色限一次，你可以发动一次向牌堆顶首张牌求牌的“<a href=':rainveil__huoshi_taoluan'>滔乱</a>”，其拒绝权交由与场上与你体力值相差唯一最大的角色执行。",
  ["#rainveil__huoshi"] = "惑世：你可以发动一次向牌堆顶首张牌求牌的“滔乱”",
  ["#rainveil__huoshi_give"] = "惑世：你可以将牌堆顶一张牌交给 %dest ，否则本回合此技能失效，其失去1点体力。",

  [":rainveil__huoshi_taoluan"] = "每种牌名限一次，当你需要使用基本牌或普通锦囊牌时，若没有角色处于濒死状态，你可以将一张牌当此牌使用，然后你令一名其他角色"..
  "选择一项：1.交给你一张类别不同的牌；2.令你失去1点体力，此技能本回合失效。",

  ["@[rainveil__huoshi]"] = "惑世",

  ["$rainveil__huoshi1"] = "天地有常法，不失铢分也。",
  ["$rainveil__huoshi2"] = "此符上格神明，下通幽府，有诸般之神效。",
  
  ["$rainveil__huoshi3"] = "（滔乱失效）哀哉！有志之士，早计早计，无负今言。",
}
Fk:addQmlMark{
  name = "rainveil__huoshi",
  how_to_show = function(_, value)
    if type(value) ~= "table" then return " " end
    if type(value.suits) ~= "table" or #value.suits == 0 then return " " end
    return table.concat(table.map(value.suits, function(s)
      return Fk:translate(s)
    end), "")
  end,
  qml_data = function(name, value, p)
    if type(value) ~= "table" then return value end
    return value.value
  end,
  qml_path = "packages/utility/qml/ViewPile"
}

huoshi:addEffect("viewas", {
  audio_index = {1,2},
  pattern = ".",
  prompt = "#rainveil__huoshi",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    local names = player:getViewAsCardNames(huoshi.name, all_names, nil, player:getTableMark("@[rainveil__huoshi]").value)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  filter_pattern = function (self, player, card_name, selected)
    local suits = {"spade", "heart", "club", "diamond"}
    local mark = player:getMark("@[rainveil__huoshi]")
    if type(mark) == "table" then
      for _, v in ipairs(mark.suits) do
        table.removeOne(suits, string.sub(v, 5))
      end
    end
    if #suits > 0 then
      return {
        min_num = 1,
        max_num = 1,
        pattern = ".|.|".. table.concat(suits, ","),
      }
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = huoshi.name
    return card
  end,
  before_use = function(self, player, use)
    local mark = player:getMark("@[rainveil__huoshi]")
    if type(mark) ~= "table" then
      mark = {
        value = {},
        suits = {},
      }
    end
    table.insert(mark.value, use.card.trueName)
    table.insert(mark.suits, use.card:getSuitString(true))
    player.room:setPlayerMark(player, "@[rainveil__huoshi]", mark)
  end,
  after_use = function(self, player, use)
    local room = player.room
    if player.dead or #room:getOtherPlayers(player, false) == 0 then return end
    local type = use.card:getTypeString()
    local card
    if #room.draw_pile > 0 then
      card = Fk:getCardById(room.draw_pile[1])
    end
    if card and card.type == type then
      card = nil
    end
    if card then
      local available_player = player
      local available = true
      for _, p in ipairs(room:getAlivePlayers(false)) do
        if p.hp - player.hp > available_player.hp - player.hp then
          available_player = p
          available = true
        elseif p.hp - player.hp == available_player.hp - player.hp then
          available = false
        end
      end
      if available then
        if not room:askToSkillInvoke(available_player, {
          skill_name = huoshi.name,
          prompt = "#rainveil__huoshi_give::"..player.id,
        }) then
          card = nil
        end
      end
    end
    if card then
      room:obtainCard(player, card, false, fk.ReasonGive, player, huoshi.name)
    elseif player:hasSkill(huoshi.name, true) then
      room:invalidateSkill(player, huoshi.name, "-turn")
      player:broadcastSkillInvoke(huoshi.name, 3)
      local mark = player:getTableMark("@[rainveil__huoshi]")
      room:loseHp(player, 1, huoshi.name)
      room:setPlayerMark(player, "@[rainveil__huoshi]", mark)
    end
  end,
  enabled_at_play = function(self, player)
    local mark = player:getMark("@[rainveil__huoshi]")
    return type(mark) ~= "table" or #mark.suits < 4 and
      table.every(Fk:currentRoom().alive_players, function(p)
        return not p.dying
      end)
  end,
  enabled_at_response = function(self, player, response)
    if response then return end
    local mark = player:getMark("@[rainveil__huoshi]")
    return type(mark) ~= "table" or
      (#mark.suits < 4 and #player:getViewAsCardNames(huoshi.name, Fk:getAllCardNames("bt"), nil, mark.value) > 0) and
      table.every(Fk:currentRoom().alive_players, function(p)
        return not p.dying
      end)
  end,
  enabled_at_nullification = function (self, player, data)
    return #player:getHandlyIds() > 0 and self:enabledAtResponse(player, false)
  end
})

huoshi:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@[rainveil__huoshi]", 0)
end)

huoshi:addEffect(fk.RoundEnd, {
  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@[rainveil__huoshi]") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getMark("@[rainveil__huoshi]")
    mark.suits = {}
    player.room:setPlayerMark(player, "@[rainveil__huoshi]", mark)
  end,
})

return huoshi